//
//  Presenter.m
//  MV_C_P_VM
//
//  Created by Willian on 2021/4/30.
//

#import "Presenter.h"
#import "CommonHeader.h"

@interface Presenter ()

//这个只做中转判断，不用来改变原模型的值
@property (nonatomic, copy) NSString *userName;
@property (nonatomic, copy) NSString *password;

@end

@implementation Presenter

- (void)refreshUI{
    BOOL enableLogin = YES;
    UIColor *usernameBgColor = nil;
    UIColor *passwordBgColor = nil;
    if ([self.userName length] >= 4 && [self.userName length] <= 16) {
        usernameBgColor = GreenBgColor;
    } else {
        if ([self.userName length] == 0) {
            usernameBgColor = WhiteBgColor;
        } else {
            usernameBgColor = RedBgColor;
        }
        enableLogin = NO;
    }
    
    if ([self.password length] >= 8 && [self.password length] <= 16) {
        passwordBgColor = GreenBgColor;
    } else {
        if ([self.password length] == 0) {
            passwordBgColor = WhiteBgColor;
        } else {
            passwordBgColor = RedBgColor;
        }
        enableLogin = NO;
    }

    if (self.delegate) {
        if ([self.delegate respondsToSelector:@selector(updateUsernameBgColor:)]) {
            [self.delegate updateUsernameBgColor:usernameBgColor];
        }
        if ([self.delegate respondsToSelector:@selector(updatePasswordBgColor:)]) {
            [self.delegate updatePasswordBgColor:passwordBgColor];
        }
        if ([self.delegate respondsToSelector:@selector(updateLoginEnabled:)]) {
            [self.delegate updateLoginEnabled:enableLogin];
        }
    }
}

- (void)updateUsername:(NSString *)username
{
    self.userName = username;
    [self refreshUI];
}

- (void)updatePassword:(NSString *)password
{
    self.password = password;
    [self refreshUI];
}

@end
